package service

import (
	"context"
	"cronjob/common"
	"cronjob/models"
	"encoding/json"
	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/logs"
	"strconv"
	"strings"
	"time"
)

// ManagerJob 管理job, 将job添加到etcd
type ManagerJob struct {
	etcdclient *common.EtcdCient
}

func NewManagerJob(ctx context.Context) (*ManagerJob, error) {
	var conn []string = beego.AppConfig.Strings("etcd.url")
	var username = beego.AppConfig.String("etcd.user")
	var password = beego.AppConfig.String("etcd.password")
	etcdclient, err := common.NewClient(ctx, conn, username, password, 35*time.Second)
	if err != nil {
		return nil, err
	}

	return &ManagerJob{
		etcdclient: etcdclient,
	}, nil
}

func (t *ManagerJob) AddjobMuti(ctx context.Context, jobs []*models.Task) bool {
	var (
		key string
	)

	mapdata, err := models.TaskServerGetByIdsMap() // 获取全部的服务器资源
	if err != nil {
		logs.Error("get server error" + err.Error())
		return false
	}

	for _, job := range jobs {
		serverIdArr := strings.Split(job.ServerIds, ",")
		if len(serverIdArr) == 0 {
			return false
		}

		for _, v := range serverIdArr {
			vid, _ := strconv.Atoi(v)
			if da, ok := mapdata[vid]; ok {
				key = fmt.Sprintf("%s/%d", da, job.Id)
				job.ServerIds = v //改写服务器端的id,写入etcd之后不再保存多列服务器id
				if data, err := json.Marshal(job); err != nil {
					logs.Error("json job失败" + err.Error())
					return false
				} else {

					if result := t.etcdclient.Adddata(ctx, key, string(data)); result != nil {
						logs.Error("添加job到etcd失败")
						return false
					}
				}
			}
		}
	}

	return true
}

func (t *ManagerJob) AddJob(ctx context.Context, job *models.Task) bool {
	var (
		key string
	)

	serverIdArr := strings.Split(job.ServerIds, ",")
	if len(serverIdArr) == 0 {
		return false
	}

	mapdata, err := models.TaskServerGetByIdsMap() // 获取全部的服务器资源
	if err != nil {
		logs.Error("get server error" + err.Error())
		return false
	}

	for _, v := range serverIdArr {
		vid, _ := strconv.Atoi(v)
		if da, ok := mapdata[vid]; ok {
			key = fmt.Sprintf("%s/%d", da, job.Id)
			job.ServerIds = v //改写服务器端的id,写入etcd之后不再保存多列服务器id
			if data, err := json.Marshal(job); err != nil {
				logs.Error("json job失败" + err.Error())
				return false
			} else {

				if result := t.etcdclient.Adddata(ctx, key, string(data)); result != nil {
					logs.Error("添加job到etcd失败")
					return false
				}
			}
		}
	}

	return true
}

func (t *ManagerJob) DelJobMuti(ctx context.Context, jobs []*models.Task) bool {
	var (
		key string
	)

	mapdata, err := models.TaskServerGetByIdsMap() // 获取全部的服务器资源
	if err != nil {
		logs.Error("get server error" + err.Error())
		return false
	}
	for _, job := range jobs {
		serverIdArr := strings.Split(job.ServerIds, ",")
		if len(serverIdArr) == 0 {
			logs.Error("get server error， server id is empty")
			return false
		}

		for _, v := range serverIdArr {
			vid, _ := strconv.Atoi(v)
			if da, ok := mapdata[vid]; ok {
				key = fmt.Sprintf("%s/%d", da, job.Id)
				if result := t.etcdclient.Del(ctx, key); result != nil {
					logs.Error("删除job到etcd失败")
					return false
				}
			}
		}
	}

	return true
}

func (t *ManagerJob) DelJob(ctx context.Context, job *models.Task) bool {
	var (
		key string
	)

	serverIdArr := strings.Split(job.ServerIds, ",")
	if len(serverIdArr) == 0 {
		logs.Error("get server error， server id is empty")
		return false
	}

	mapdata, err := models.TaskServerGetByIdsMap() // 获取全部的服务器资源
	if err != nil {
		logs.Error("get server error" + err.Error())
		return false
	}

	for _, v := range serverIdArr {
		vid, _ := strconv.Atoi(v)
		if da, ok := mapdata[vid]; ok {
			key = fmt.Sprintf("%s/%d", da, job.Id)
			if result := t.etcdclient.Del(ctx, key); result != nil {
				logs.Error("删除job到etcd失败")
				return false
			}
		}
	}

	return true
}
